//index.js
const request = require('../../utils/request.js')
const getDate = require('../../utils/getDate.js')
const hanToast = require('../../utils/toast.js')
Page({
  onShow:function(){
    if (!wx.getStorageSync('shopID')) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return
    }
    // 获取初始化已预约的订单 数据页码归1
    this.setData({ pageKey: 1, list: [] }, () => this.getInitData())
  },
 
  data: {
    tab: [
      {
        name:"已预约",
        status:'ok'
      },
      {
        name: "已取消",
        status: 'cancel'
      },
      {
        name: "已完成",
        status: 'done'
      }],
    defaultStatus:'ok',
    list:[],
    pageKey:1,
    allPageKey:1
  },

  // 点击选项卡 数据页码归1
  clickTab:function(e){
    this.setData({ 
      defaultStatus: e.currentTarget.dataset.status,
      pageKey:1,
      list:[]
      },()=>this.getInitData())
  },

  // 确认已到店 数据页码归1
  sureGoShop(e){
    let that = this;
    wx.showModal({
      title: '系统提示',
      content: '确认该顾客已到店了吗',
      success(res) {
        if (res.confirm) {
          request(['/store/setstatus', 'POST', {
            orderid:e.currentTarget.dataset.orderid
          }], res => that.setData({ pageKey: 1, list: []}, () => that.getInitData())
          
          )
        }
      }
    })
  },

  // 页码数为1时 请求数据
  getInitData(){
    request(['/store/order', 'POST', {
      shopID: wx.getStorageSync('shopID'),
      status: this.data.defaultStatus,
      pageKey: this.data.pageKey
    }], 
    res => {
      if (res.code === 1) {
        let newlist = res.data.data.map(item=>({...item,appointmentTime:getDate(item.appointmentTime)}))
        const {list} = this.data;
        
        this.setData({ list: [...list, ...newlist], allPageKey: this.data.allPageKey })
      }
    })
  },


  onPullDownRefresh: function () {
    //下拉刷新 更新数据 数据页码归1
    this.setData({ pageKey: 1, list: []}, () => this.getInitData())
    
  },
  onReachBottom: function () {
    
    let { pageKey, allPageKey} = this.data;
    // 页面触底时执行
    if (pageKey > allPageKey) return hanToast('已经没有更多数据了')

    this.setData({ pageKey: ++pageKey }, () => this.getInitData())
  }
})
